package team.biocar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.biocar.mapper.BalanceMapper;
import team.biocar.pojo.Balance;
import team.biocar.service.BalanceService;

import java.util.Date;
import java.util.List;

/**
 * @author DeSen Xu
 * @date 2021-11-09 22:44
 */
@Service
public class BalanceServiceImpl implements BalanceService {

    // 用setter注入BalanceMapper
    private BalanceMapper balanceMapper;
    @Autowired
    public void setBalanceMapper(BalanceMapper balanceMapper){
        this.balanceMapper=balanceMapper;
    }

    @Override
    public Balance queryBalance(int id) {
        return balanceMapper.selectById(id);
    }

    @Override
    public int getSumOfTheDay(Date date) {
        // 不会写warpper就先去写SQL!
        QueryWrapper<Balance> Wrapper = new QueryWrapper<>();
        QueryWrapper<Balance> select = Wrapper.select("sum(CASE balance_type WHEN 1 THEN revenue ELSE -revenue END ").eq(BalanceMapper.COLUMN_DATE,date);
         return balanceMapper.selectCount(select).intValue();

    }

    @Override
    public Balance query(int id) {
        return balanceMapper.query(id);
    }


}
